package io.renren.modules.nuaa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.nuaa.dao.TCategoryDao;
import io.renren.modules.nuaa.dao.TCertificateDao;
import io.renren.modules.nuaa.dto.TCertificateDTO;
import io.renren.modules.nuaa.entity.TCategoryEntity;
import io.renren.modules.nuaa.entity.TCertificateEntity;
import io.renren.modules.nuaa.service.TCertificateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 证书表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 3.0 2023-11-07
 */
@Service
public class TCertificateServiceImpl extends CrudServiceImpl<TCertificateDao, TCertificateEntity, TCertificateDTO> implements TCertificateService {

    @Autowired
    private TCategoryDao tCategoryDao;

    @Override
    public QueryWrapper<TCertificateEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<TCertificateEntity> wrapper = new QueryWrapper<>();
        String name = (String) params.get("name");
        String idCard = (String) params.get("idCard");
        String certificateNo = (String) params.get("certificateNo");
        String categoryId = (String) params.get("categoryId");

        if(StringUtils.isNotBlank(name)){
            wrapper.like("name",name);
        }
        if(StringUtils.isNotBlank(idCard)){
            wrapper.eq("id_card",idCard);
        }
        if(StringUtils.isNotBlank(certificateNo)){
            wrapper.eq("certificate_no",certificateNo);
        }
        if(StringUtils.isNotBlank(categoryId)){
            wrapper.eq("category_id",Integer.parseInt(categoryId));
        }


        return wrapper;
    }


    @Override
    public List<TCertificateDTO> queryListByNameAndIDCard(Map<String, Object> params) {

        QueryWrapper<TCertificateEntity> wrapper = new QueryWrapper<>();
        String name = (String)params.get("userName");
        String idCard = (String)params.get("idCard");
        String categoryId = (String)params.get("categoryId");
        if(StringUtils.isNotEmpty(name)){
            wrapper.eq("name",name);
        }
        if(StringUtils.isNotEmpty(idCard)){
            wrapper.eq("id_card",idCard);
        }
        if(StringUtils.isNotEmpty(categoryId)){
            wrapper.eq("category_id",Integer.parseInt(categoryId));
        }

        List<TCertificateEntity> list = baseDao.selectList(wrapper);

        return ConvertUtils.sourceToTarget(list, currentDtoClass());
    }

    @Override
    public Boolean queryByConditions(Integer categoryId, String idCard, String certificateNo) {
        QueryWrapper<TCertificateEntity> wrapper = new QueryWrapper<TCertificateEntity>().eq("category_id",categoryId);

        if(StringUtils.isNotBlank(idCard) && StringUtils.isNotBlank(certificateNo)){
            wrapper.and(w -> w.eq("id_card",idCard).or().eq("certificate_no",certificateNo));
        }else if(StringUtils.isNotBlank(idCard) && StringUtils.isBlank(certificateNo)){
            wrapper.eq("id_card",idCard);
        }else if(StringUtils.isNotBlank(certificateNo) && StringUtils.isBlank(idCard)){
            wrapper.eq("certificate_no",certificateNo);
        }

        TCertificateEntity tCertificateEntity = baseDao.selectOne(wrapper);
        if(tCertificateEntity == null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<TCertificateDTO> queryListForResult(String name, String idCard) {

        if(StringUtils.isBlank(name) || StringUtils.isBlank(idCard)){
            return null;
        }

        List<TCertificateEntity> list = baseDao.selectList(new QueryWrapper<TCertificateEntity>().eq("name", name).eq("id_card", idCard));

        list.forEach(e ->{
            if(e.getCategoryId() != null){
                TCategoryEntity tCategoryEntity = tCategoryDao.selectById((long)e.getCategoryId());
                if(tCategoryEntity != null){
                    e.setCategoryName(tCategoryEntity.getCategoryName());
                }
            }
        });

        return ConvertUtils.sourceToTarget(list, currentDtoClass());
    }
}